<template>
  <el-dialog
    title="发送消息"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="65px"
      label-position="top"
    >
      <el-form-item label="消息模板" label-width="120" prop="templateId">
        <el-select v-model="dataForm.templateId">
          <el-option
            v-for="item in templates"
            :key="item.id"
            :label="item.templateName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="消息" prop="content">
        <el-input
          type="textarea"
          v-model="dataForm.content"
          placeholder="消息"
          style="height: 100%"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="send()">发送</el-button>
    </span>
  </el-dialog>
</template>

<script>
//import queryObject, { queryObj } from "../../utils/queryObject";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        templateId: "",
        content: "",
      },
      message: {
        sender_id: '',
        recipient_id: '',
        title: '',
        content: "",
        jump_type: '',
        jump_url:'',
      },
      templates: [
      ],
      dataRule: {
        templateId: [
          { required: true, message: "模板不能为空", trigger: "blur" },
        ],
        content: [{ required: true, message: "消息不能为空", trigger: "blur" }],
      },
    };
  },

  methods: {
    init(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        this.$http({
          url: this.$http.adornUrl("/forum/messagetemplate/list"),
          method: "get",
          params: this.$http.adornParams({
            'page': 1,
            'limit': 100,
            'key': this.dataForm.key
          }),
        }).then(({ data }) => {
          if (data && data.code === 0) {
              this.templates=data.page.list
          }
        });
      });
    },
    /** 当用户点击发送时，根据他选择的模板以及填写的内容组成  消息
     *  去请求发送message
     *
     *
     *
     */
    send() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
            this.$http({
                url: this.$http.adornUrl(`/forum/membermessage/save`),
                method: "post",
                data: this.$http.adornData(this.message),
            }).then(({ data }) => {
                if (data && data.code === 0) {
                this.$message({
                    message: "发送成功",
                    type: "success",
                    duration: 1500,
                    onClose: () => {
                      this.visible = false;
                    },
                });
                } else {
                this.$message.error(data.msg);
                }
            });
        }
      })
    },
  },
};
</script>
